#!/usr/bin/python
#
# -*- coding: utf-8 -*-

# Copyright 2011 Google Inc. All Rights Reserved.

"""One-line documentation for b module.

A detailed description of b.
"""

from BeautifulSoup import BeautifulSoup as BS
from BeautifulSoup import Tag
import urllib


def briscity(text, time):
  for line in text.split('\n'):
    line = line.strip('"').strip()
    if line.startswith('Due to'): continue
    line = line.replace('\xe2\x80\x93'.decode('utf-8'),'-')
    if '-' not in line: continue
    suburb, road = line.split('-', 1)
    roads = road.split(',')
    for road in roads:
      road = road.strip()
      if not road: continue
      location = "%s, %s, brisbane, queensland" %(road, suburb)
      out = '"%s","%s %s","%s",""' % (
                location, road, suburb, time)
      print out.encode('utf-8')

def scrapyscrapy():
  bs = BS(urllib.urlopen("http://highload.131940.qld.gov.au/").read())
  #bs = BS(open('roads.html'))

  body = bs.fetch('div', {'id':'body'})

  entries = body[0].findAll('li')

  for entry in entries:
    if entry.findAll('span', {'class':'suburb'}):
      tag = entry.next
      while tag is not None:
        tag = tag.next
        if isinstance(entry, Tag):
          if hasattr(tag, 'name') and tag.name == 'span':
            if tag['class'] == 'suburb':
              suburb = str(tag.contents[0]).strip()
              tag = tag.nextSibling # road name
              road = str(tag).strip('-').strip()
              tag = tag.nextSibling # time
              time = str(tag.contents[0]).strip()
              while True:
                tag = tag.next
                if hasattr(tag, 'name') and tag.name == 'p':
                  break

            if suburb != "BRISBANE CITY":
              text = tag.contents[0].string.strip()
              out = '"%s, %s, Queensland, Australia","%s %s","%s","%s"' % (
                  road, suburb, road, suburb, time, text)
              print out.encode('utf-8')
            else:
              text = ''.join(x.string for x in tag.contents if x.string)
              text = text.replace('<br>', '\n')
              briscity(text, time)
            break




if __name__ == "__main__":
  scrapyscrapy()
